#include "stdint.h"
#include "string.h"
#include "mcquay_air_condition.h"

AC_Mcquay_DeviceInfo_Typedef MQ_DeviceList[AC_DEVICE_Num];

#define SET_06_FUNC_IDX (0x00u)
#define SET_06_ADDRH_IDX (0x01u)
#define SET_06_ADDRL_IDX (0x02u)
#define SET_06_DATAH_IDX (0x03u)
#define SET_06_DATAL_IDX (0x04u)

void mcquay_parse_sensor_status( uint16_t start_addr, uint8_t * data, uint8_t data_len )
{
    uint16_t current_addr = start_addr;
    uint16_t left_data = data_len/2;

    while(left_data != 0)
    {
        /* 04 Imput reg */
        if(current_addr >= 6000 && current_addr <= 6639)
        {
            uint8_t device_id = ((current_addr - 6000) / 20) + 1;
            if(device_id < AC_DEVICE_Num)
            {               
                
                uint16_t index = data_len - left_data * 2; 
                uint16_t value = ((uint16_t)data[index] << 8) | (uint16_t)data[index + 1];
                switch(current_addr - (device_id - 1) * 20)
                {
                    case 0:
                    {
                        MQ_DeviceList[device_id].RoomTemperature = (int16_t)value;
                        break;
                    }
                    case 1: /* 运行模式 */
                    {
                        if(value != 1 && value != 2 && value != 4 && value != 8 && value != 18 && value != 32 && value != 64)
                        {
                            MQ_DeviceList[device_id].CurrentMode = 0;
                        }
                        else
                        {
                            MQ_DeviceList[device_id].CurrentMode = value;
                        }
                        break;
                    }
                    case 2: 
                    {
                        if(value < 1 || value > 7)
                        {
                            MQ_DeviceList[device_id].CurrentSpeed = 0;
                        }
                        else
                        {
                            MQ_DeviceList[device_id].CurrentSpeed = value;
                        }
                        break;
                    }
                    case 3:
                    {
                        
                        MQ_DeviceList[device_id].CurrentSwitch = value & 0x01;
                        MQ_DeviceList[device_id].VIPMode = (value & 0x02) >> 1;
                        break;
                    }
                    case 4:
                    {
                        MQ_DeviceList[device_id].NetOnlineStatus = value & 0x01;
                        break;
                    }
                    case 5:
                    {
                        MQ_DeviceList[device_id].SetHumidityReadBack = value;
                        break;
                    }
                    case 6:
                    {
                        uint16_t speed = value & 0xFF;
                        if(speed < 1 || (speed > 7 && speed != 128))
                        {
                            MQ_DeviceList[device_id].TemperatureControlerSetSpeedReadBack = 0;
                        }
                        else
                        {
                            MQ_DeviceList[device_id].TemperatureControlerSetSpeedReadBack = speed;
                        }
                        MQ_DeviceList[device_id].SetSleepReadBack = (value >> 12) & 0x01;
                        MQ_DeviceList[device_id].SetSwitchReadBack = (value >> 13) & 0x01;
                        MQ_DeviceList[device_id].TemperatureControlMode = (value >> 14) & 0x01;
                        MQ_DeviceList[device_id].IsSupportDehumidificationReheating = (value >> 15) & 0x01;
                        break;
                    }
                    case 7:
                    {
                        uint16_t mode = (value & 0xFF00) >> 8;
                        uint16_t speed = value & 0xFF;
                        if(mode != 1 && mode != 2 && mode != 4 && mode != 8 && mode != 16 && mode != 18 && mode != 32 && mode != 64)
                        {
                            MQ_DeviceList[device_id].SetModeReadBack = 0;
                        }
                        else
                        {
                            MQ_DeviceList[device_id].SetModeReadBack = mode;
                        }
                        
                        if(speed < 1 || (speed > 7 && speed != 128))
                        {
                            MQ_DeviceList[device_id].SetSpeedReadBack = 0;
                        }
                        else
                        {
                            MQ_DeviceList[device_id].SetSpeedReadBack = speed;
                        }
                        break;
                    }
                    case 8:
                    {
                        if(value >= 160 && value <= 300)
                        {
                            MQ_DeviceList[device_id].SetTemperatureReadBack = value;
                        }
                        else
                        {
                            MQ_DeviceList[device_id].SetTemperatureReadBack = 0;
                        }
                        break;
                    }
                    case 9:
                    {
                        if(value >= 100 && value <= 100)
                        {
                            MQ_DeviceList[device_id].SetHumidityReadBack = value;
                        }
                        else
                        {
                            MQ_DeviceList[device_id].SetHumidityReadBack = 0;
                        }
                        break;
                    }
                    case 10:
                    {
                        if(value == 0xFF || value <= 0x0E)
                        {
                            MQ_DeviceList[device_id].SetLockReadBack = value;
                        }
                        else
                        {
                            MQ_DeviceList[device_id].SetLockReadBack = 0;
                        }
                        break;
                    }
                    case 11:
                    {
                        MQ_DeviceList[device_id].SupportRefrigeration = value & 0x01;
                        MQ_DeviceList[device_id].SupportHeating = (value >> 1) & 0x01;
                        MQ_DeviceList[device_id].SupportAirsupply = (value >> 2) & 0x01;
                        MQ_DeviceList[device_id].SupportAutomatic = (value >> 3) & 0x01;
                        MQ_DeviceList[device_id].SupportFloorheating = (value >> 4) & 0x01;
                        MQ_DeviceList[device_id].SupportDehumidificatio = (value >> 5) & 0x01;
                        MQ_DeviceList[device_id].SupportDehumidification_Reheating = (value >> 6) & 0x01;

                        break;
                    }
                    case 12:
                    {
                        MQ_DeviceList[device_id].HasFault = value;

                        break;
                    }
                    case 13:
                    {
                        MQ_DeviceList[device_id].MainDeviceDriver1Fault = value;

                        break;
                    }
                    case 14:
                    {
                        MQ_DeviceList[device_id].MainDeviceDriver2Fault = value;

                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                
                current_addr++;
                left_data--;
            }
            else
            {
                current_addr++;
                left_data--;
            }
        }
        else
        {
            current_addr++;
            left_data--;
        }
    }



}

uint8_t mcquay_tx_msg_package( AC_DEVICE_Typedef device, AC_REG_06_Typedef reg, uint16_t data, uint8_t * payload)
{
    uint8_t ret = 0;
    uint8_t tx_flag = 1;

    if(device < AC_DEVICE_Num)
    {
        
        uint16_t reg_idx = (uint8_t)reg;
        payload[SET_06_FUNC_IDX] = 0x06;
        payload[SET_06_ADDRH_IDX] = (uint8_t)0xFF;
        payload[SET_06_ADDRL_IDX] = (uint8_t)0xFF;
        payload[SET_06_DATAH_IDX] = 0x00;
        payload[SET_06_DATAL_IDX] = 0x00;
        switch( reg )
        {
            case AC_REG_06_1000_Switch:
            {
                if(data == 0)
                {
                    payload[SET_06_DATAL_IDX] = 0x00;
                }
                else if(data == 1)
                {
                    payload[SET_06_DATAL_IDX] = 0x01;
                }
                else
                {
                    tx_flag = 0;
                }
                break;
            }
            case AC_REG_06_1001_Mode:
            {
                if(data != 1 && data != 2 && data != 4 && data != 8 && data != 16 && data != 18 && data != 32 && data != 64)
                {
                    tx_flag = 0;
                }
                else
                {
                    payload[SET_06_DATAL_IDX] = (uint8_t)data;
                }
                break;
            }
            case AC_REG_06_1002_Speed:
            {
                
                if(data < 1 || (data > 7 && data != 128))
                {
                    tx_flag = 0;
                }
                else
                {
                    payload[SET_06_DATAL_IDX] = (uint8_t)data;
                }
                break;
            }
            case AC_REG_06_1003_Tmp:
            {
                
                if(data >= 160 && data <= 300)
                {
                    payload[SET_06_DATAH_IDX] |= (uint8_t)((data >> 8) & 0xFF);
                    payload[SET_06_DATAL_IDX] = (uint8_t)(data & 0xFF);
                }
                else
                {
                    tx_flag = 0;
                }
                break;
            }
            case AC_REG_06_1004_Humidity:
            {
                if(data == 0)
                {
                    payload[4] = 0x00;
                }
                else
                {
                    payload[4] = 0x01;
                }
                break;
            }
            case AC_REG_06_1004_Sleep:
            {
                if(data == 0)
                {
                    payload[4] = 0x00;
                }
                else
                {
                    payload[4] = 0x01;
                }
                reg_idx = 4;
                break;
            }
            case AC_REG_06_1004_Swing:
            {
                if(data == 0)
                {
                    payload[4] = 0x00;
                }
                else
                {
                    payload[4] = 0x01;
                }
                reg_idx = 4;
                break;
            }
            case AC_REG_06_1005_Lock:
            {
                if(data == 0)
                {
                    payload[4] = 0x00;
                }
                else
                {
                    payload[4] = 0x01;
                }
                reg_idx = 5;
                break;
            }
            default:
            {
                if(data == 0)
                {
                    payload[4] = 0x00;
                }
                else
                {
                    payload[4] = 0x01;
                }
                break;
            }
        }

        if(tx_flag != 0)
        {
            
            uint16_t addr = device * 11 + reg_idx;
            payload[SET_06_ADDRH_IDX] = (uint8_t)((addr >> 8) & 0xFF);
            payload[SET_06_ADDRL_IDX] = (uint8_t)(addr & 0xFF);
            payload[SET_06_DATAH_IDX] |= 0x80;
        }
            
    }
}


